package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.entity.SmsLoginParams;
import com.itheima.health.pojo.Member;
import com.itheima.health.service.MemberService;
import com.itheima.health.service.SmsLoginService;
import com.itheima.health.service.SmsRocketMQService;
import com.itheima.health.service.SmsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;


@Slf4j
@RestController
@RequestMapping("/mobile/login")
public class LoginController {

//    @DubboReference
//    private SmsLoginService smsLoginService;
    //@DubboReference
   // private SmsService smsService;
    @DubboReference
    private MemberService memberService;
    @DubboReference
    private SmsRocketMQService smsRocketMQService;

    @PostMapping("/smsLogin")
    public Result smsLogin(@RequestParam("type") String type,@RequestBody SmsLoginParams smsLoginParams) {
        /*
         * @Date: 8:12 下午 2021/1/11
         * @param: [smsLoginParams]
         * @return: com.itheima.health.entity.Result
         * @Description: 判断验证码是否正确，再判断数据库是否有这个用户
         */
        log.info("[登录-短信登录-smsLogin-前端传入数据] - smsLoginParams: {}", smsLoginParams);
        String phone = smsLoginParams.getTelephone();
        boolean flag = smsRocketMQService.checkValidateCode(phone, smsLoginParams.getValidateCode(),type);
        if (!flag) {
            return new Result(false, MessageConst.VALIDATECODE_ERROR);
        }

        Member member = new Member();
        member.setPhoneNumber(smsLoginParams.getTelephone());
        member = memberService.addMember(member);
        if (member.getId()==null){
            return new Result(false,MessageConst.LOGIN_FAIL);
        }
        return new Result(true, MessageConst.LOGIN_SUCCESS);
    }
}
